package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbOrganizer;
import com.ruoyi.system.mapper.TbOrganizerTypeMapper;
import com.ruoyi.system.domain.TbOrganizerType;
import com.ruoyi.system.service.ITbOrganizerTypeService;

/**
 * 组织者类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbOrganizerTypeServiceImpl implements ITbOrganizerTypeService 
{
    @Autowired
    private TbOrganizerTypeMapper tbOrganizerTypeMapper;

    /**
     * 查询组织者类型
     * 
     * @param id 组织者类型主键
     * @return 组织者类型
     */
    @Override
    public TbOrganizerType selectTbOrganizerTypeById(Long id)
    {
        return tbOrganizerTypeMapper.selectTbOrganizerTypeById(id);
    }

    /**
     * 查询组织者类型列表
     * 
     * @param tbOrganizerType 组织者类型
     * @return 组织者类型
     */
    @Override
    public List<TbOrganizerType> selectTbOrganizerTypeList(TbOrganizerType tbOrganizerType)
    {
        return tbOrganizerTypeMapper.selectTbOrganizerTypeList(tbOrganizerType);
    }

    /**
     * 新增组织者类型
     * 
     * @param tbOrganizerType 组织者类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbOrganizerType(TbOrganizerType tbOrganizerType)
    {
        // 只保留icon字段的第一个url
        if (tbOrganizerType.getIcon() != null && tbOrganizerType.getIcon().contains(",")) {
            tbOrganizerType.setIcon(tbOrganizerType.getIcon().split(",")[0]);
        }
        int rows = tbOrganizerTypeMapper.insertTbOrganizerType(tbOrganizerType);
        insertTbOrganizer(tbOrganizerType);
        return rows;
    }

    /**
     * 修改组织者类型
     * 
     * @param tbOrganizerType 组织者类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbOrganizerType(TbOrganizerType tbOrganizerType)
    {
        // 只保留icon字段的第一个url
        if (tbOrganizerType.getIcon() != null && tbOrganizerType.getIcon().contains(",")) {
            tbOrganizerType.setIcon(tbOrganizerType.getIcon().split(",")[0]);
        }
        tbOrganizerTypeMapper.deleteTbOrganizerByTypeId(tbOrganizerType.getId());
        insertTbOrganizer(tbOrganizerType);
        return tbOrganizerTypeMapper.updateTbOrganizerType(tbOrganizerType);
    }

    /**
     * 批量删除组织者类型
     * 
     * @param ids 需要删除的组织者类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOrganizerTypeByIds(Long[] ids)
    {
        tbOrganizerTypeMapper.deleteTbOrganizerByTypeIds(ids);
        return tbOrganizerTypeMapper.deleteTbOrganizerTypeByIds(ids);
    }

    /**
     * 删除组织者类型信息
     * 
     * @param id 组织者类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOrganizerTypeById(Long id)
    {
        tbOrganizerTypeMapper.deleteTbOrganizerByTypeId(id);
        return tbOrganizerTypeMapper.deleteTbOrganizerTypeById(id);
    }

    /**
     * 新增组织者管理信息
     * 
     * @param tbOrganizerType 组织者类型对象
     */
    public void insertTbOrganizer(TbOrganizerType tbOrganizerType)
    {
        List<TbOrganizer> tbOrganizerList = tbOrganizerType.getTbOrganizerList();
        Long id = tbOrganizerType.getId();
        if (StringUtils.isNotNull(tbOrganizerList))
        {
            List<TbOrganizer> list = new ArrayList<TbOrganizer>();
            for (TbOrganizer tbOrganizer : tbOrganizerList)
            {
                tbOrganizer.setTypeId(id);
                list.add(tbOrganizer);
            }
            if (list.size() > 0)
            {
                tbOrganizerTypeMapper.batchTbOrganizer(list);
            }
        }
    }
}
